#!/usr/bin/env -S node -r ./livescript-transform-implicit-async/register


escape = (unsafe) ~>
  unsafe
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")

module.exports = (host, title, li)~>
  t = []

  for [h1, html, url, date] in li
    url = host+url
    if date
      update = "<updated>#date</updated>"
    else
      update = ''
    t.push """<entry>#{update}<title type="html"><![CDATA[#{escape h1}]]></title><link href="#url"/><id>#url</id><content type="html"><![CDATA[#html]]></content></entry>"""

  """<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[#{escape title}]]></title>
<id>#host</id><link
href="#host/atom.xml" rel="self"/><link
href="#host"/><updated>#{(new Date()).toISOString!}</updated>#{t.join('')}
</feed>"""
